//
//  RegistViewController.m
//  UI12豆瓣大项目
//
//  Created by dllo on 15/10/29.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//


#import "RegistViewController.h"
#import "User.h"
#import "ActivitySinglton.h"

@interface RegistViewController ()<UITextFieldDelegate>
@property (nonatomic,retain) UILabel *nameLabel;
@property (nonatomic,retain) UILabel *passwordLabel;
@property (nonatomic,retain) UILabel *repasswordLabel;
@property (nonatomic,retain) UILabel *emailLabel;
@property (nonatomic,retain) UILabel *connectLabel;
@property (nonatomic,retain) UITextField *nameField;
@property (nonatomic,retain) UITextField *passwordField;
@property (nonatomic,retain) UITextField *repasswordField;
@property (nonatomic,retain) UITextField *emailField;
@property (nonatomic,retain) UITextField *connectField;
@end

@implementation RegistViewController
- (void)dealloc
{
    [_nameLabel release];
    [_passwordLabel release];
    [_repasswordLabel release];
    [_emailLabel release];
    [_connectLabel release];
    [_nameField release];
    [_passwordField release];
    [_emailField release];
    [_connectField release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //左导航键
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"用户登录" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarbuttonAction:)];
    
    //右导航键
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarbuttonAction:)];
    
    //用户名
    self.nameField = [[UITextField alloc] initWithFrame:CGRectMake(150, 80, 200, 60)];
    self.nameField.placeholder = @"请输入用户名";
    self.nameField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.nameField];
    [_nameField release];
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 80, 100, 60)];
    self.nameLabel.text = @"用户名";
    [self.view addSubview:self.nameLabel];
    [_nameLabel release];
    
    //密码
    self.passwordField = [[UITextField alloc] initWithFrame:CGRectMake(150, 160, 200, 60)];
    self.passwordField.placeholder = @"请输入密码";
    //隐藏密码!
    self.passwordField.secureTextEntry = YES;
    self.passwordField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.passwordField];
    [_passwordField release];
    
    self.passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 160, 100, 60)];
    self.passwordLabel.text = @"密码";
    [self.view addSubview:self.passwordLabel];
    [_passwordLabel release];
    
    //确认密码
    self.repasswordField = [[UITextField alloc] initWithFrame:CGRectMake(150, 240, 200, 60)];
    //隐藏密码!
    self.repasswordField.secureTextEntry = YES;
    self.repasswordField.placeholder = @"请再次输入密码";
    self.repasswordField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.repasswordField];
    [_repasswordField release];
    
    self.repasswordLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 240, 100, 60)];
    self.repasswordLabel.text = @"确认密码";
    [self.view addSubview:self.repasswordLabel];
    [_repasswordLabel release];
    
    //邮箱
    self.emailField = [[UITextField alloc] initWithFrame:CGRectMake(150, 320, 200, 60)];
    self.emailField.placeholder = @"请输入常用邮箱";
    self.emailField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.emailField];
    [_emailField release];
    
    self.emailLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 320, 100, 60)];
    self.emailLabel.text = @"邮箱";
    [self.view addSubview:self.emailLabel];
    [_emailLabel release];
    
    //联系方式
    self.connectField = [[UITextField alloc] initWithFrame:CGRectMake(150, 400, 200, 60)];
    self.connectField.placeholder = @"请输入常用邮箱";
    self.connectField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.connectField];
    [_connectField release];
    
    self.connectLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 400, 100, 60)];
    self.connectLabel.text = @"联系方式";
    [self.view addSubview:self.connectLabel];
    [_connectLabel release];
    
    
    //    _nameField = nil;
    //    _passwordField = nil;
    //    _readdelegate = nil;
    //    _emailField = nil;
    //    _connectField = nil;
    
    //回收键盘
    _nameField.delegate = self;
    _passwordField.delegate = self;
    _emailField.delegate = self;
    _connectField.delegate = self;
    _repasswordField.delegate = self;
    // Do any additional setup after loading the view.
}

//回收键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    return YES;
}


//注册
-(void)rightBarbuttonAction:(UIBarButtonItem *)button{
    
    [self saveData];
    
}

//用户登录
-(void)leftBarbuttonAction:(UIBarButtonItem *)button{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}

//储存数据
-(void)saveData{
    
    [[ActivitySinglton shareDataBaseSingleton] openDB];
    [[ActivitySinglton shareDataBaseSingleton] createUserTable];
    User *user_1 = [[User alloc] init];
    user_1.name = self.nameField.text;
    user_1.password = self.passwordField.text;
#warning 思路!
    //查找对应对象
    User *tempUser = [[ActivitySinglton shareDataBaseSingleton] selectUserWithTitle:self.nameField.text];
    //判断姓名是否相同
#warning 先判断空吧!!!
    if (self.nameField.text.length == 0 ||self.passwordField.text.length == 0) {
        UIAlertController *Alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"账号密码不能为空" preferredStyle:UIAlertControllerStyleAlert];
        
        [Alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        }]];
        [self presentViewController:Alert animated:YES completion:^{
        }];
    }
    else if ([tempUser.name isEqualToString: self.nameField.text]) {
        
        UIAlertController *Alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名已经被注册" preferredStyle:UIAlertControllerStyleAlert];
        
        [Alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        
        [self presentViewController:Alert animated:YES completion:^{
        }];
    }
    else if (![self.passwordField.text isEqualToString:self.repasswordField.text]){
        
        UIAlertController *Alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"两次密码输入不相同" preferredStyle:UIAlertControllerStyleAlert];
        
        [Alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        }]];
        [self presentViewController:Alert animated:YES completion:^{
        }];
    }
    else{
        UIAlertController *Alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功" preferredStyle:UIAlertControllerStyleAlert];
        
        [Alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
#warning 顺序!!
            [self dismissViewControllerAnimated:YES completion:^{
                
                
            }];
            
        }]];
        [self presentViewController:Alert animated:YES completion:^{
            
        }];
        [[ActivitySinglton shareDataBaseSingleton] insertUser:user_1];
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
